package com.jqh.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jqh.demo.entity.AclUser;
import com.jqh.demo.entity.AclUserRole;
import com.jqh.demo.mapper.AclUserMapper;
import com.jqh.demo.service.AclRoleMenuService;
import com.jqh.demo.service.AclUserRoleService;
import com.jqh.demo.service.AclUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jqh.demo.utils.JwtUtils;
import com.jqh.demo.utils.MD5;
import com.jqh.demo.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author jqh
 * @since 2021-05-08
 */
@Service
public class AclUserServiceImpl extends ServiceImpl<AclUserMapper, AclUser> implements AclUserService {

    @Autowired
    private AclUserRoleService aclUserRoleService;

    @Autowired
    private AclRoleMenuService aclRoleMenuService;



    @Override
    public void pageQuery(Page<AclUser> page, String userName) {
        QueryWrapper<AclUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        if(StringUtils.isEmpty(userName)){
            baseMapper.selectPage(page,queryWrapper);
            return;
        }
        queryWrapper.like("user_name",userName);
        baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public void giveUserRole(String userId, String[] roleIds) {
        //分配之前删除
        aclUserRoleService.remove(new QueryWrapper<AclUserRole>().eq("user_id",userId));
        List<AclUserRole> list=new ArrayList<>();
        for (String roleId : roleIds) {
            AclUserRole aclUserRole=new AclUserRole();
            aclUserRole.setUserId(userId);
            aclUserRole.setRoleId(roleId);
            list.add(aclUserRole);
        }
        aclUserRoleService.saveBatch(list);
    }

    @Override
    public R login(AclUser aclUser) {
        String userName = aclUser.getUserName();
        String password = aclUser.getPassword();
        if(StringUtils.isEmpty(userName)||StringUtils.isEmpty(password)) {
            return R.error().message("用户名或密码为空!");
        }
        AclUser user = baseMapper.selectOne(new QueryWrapper<AclUser>()
                                .eq("user_name", userName)
                                .eq("password", MD5.encrypt(password)));
        if(user==null){
            return R.error().message("查询无此用户或密码错误!");
        }
        String token = JwtUtils.getJwtToken(user.getId(), user.getUserName());
        return R.ok().data("token",token);
    }

    @Override
    public boolean delUserWithRelation(String id) {
        aclUserRoleService.remove(new QueryWrapper<AclUserRole>().eq("user_id", id));
        baseMapper.deleteById(id);
        return true;
    }

    @Override
    public boolean removeByIdsWithMid(List<String> idList) {
        for (String id : idList) {
            aclUserRoleService.remove(new QueryWrapper<AclUserRole>().eq("user_id",id));
        }
        int i = baseMapper.deleteBatchIds(idList);
        if(i>0){
            return true;
        }
        return false;
    }


}
